package com.kingdee.eas.hr.rec.client;

import java.awt.event.ActionEvent;
import java.util.HashSet;

import com.kingdee.bos.ctrl.extendcontrols.KDBizPromptBox;
import com.kingdee.bos.ctrl.extendcontrols.KDCommonPromptDialog;
import com.kingdee.bos.ctrl.extendcontrols.QueryAgent;
import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.kdf.table.KDTDefaultCellEditor;
import com.kingdee.bos.ctrl.kdf.table.KDTable;
import com.kingdee.bos.ctrl.kdf.table.event.KDTEditEvent;
import com.kingdee.bos.ctrl.kdf.table.event.KDTPropertyChangeEvent;
import com.kingdee.bos.ctrl.kdf.table.event.KDTPropertyChangeListener;
import com.kingdee.bos.ctrl.swing.KDComboBox;
import com.kingdee.bos.ctrl.swing.KDTextField;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.metadata.MetaDataPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.custom.common.OrgShortNameRender;
import com.kingdee.eas.framework.DataBaseInfo;
import com.kingdee.eas.hr.affair.ApproveStateEnum;
import com.kingdee.eas.hr.rec.OutPositionInfo;
import com.kingdee.eas.hr.rec.RequireApplyBillCollection;
import com.kingdee.eas.hr.rec.RequireApplyBillEntryCollection;
import com.kingdee.eas.hr.rec.RequireApplyBillEntryInfo;
import com.kingdee.eas.hr.rec.RequireApplyBillFactory;
import com.kingdee.eas.hr.rec.RequireApplyBillInfo;
import com.kingdee.eas.hr.rec.util.RecUtils;
import com.kingdee.eas.util.client.ExceptionHandler;
import com.kingdee.util.StringUtils;
import com.kingdee.util.enums.EnumUtils;

public class RequireApplyBillEditUIPIEx extends RequireApplyBillEditUI {

	private String queryFullName = "com.kingdee.eas.hr.rec.app.RequireApplyF7Query";

	private RequireApplyBillInfo cloneData;
	
	public RequireApplyBillEditUIPIEx() throws Exception {
		super();
		jbInit();
	}

	protected void jbInit() {
		KDComboBox kdtEntries_reason_ComboBox = new KDComboBox();

		kdtEntries_reason_ComboBox.addItems(EnumUtils.getEnumList(
				"com.kingdee.eas.hr.rec.ApplyResonEnum").toArray());
		KDTDefaultCellEditor kdtEntries_reason_CellEditor = new KDTDefaultCellEditor(
				kdtEntries_reason_ComboBox);
		this.kdtEntries.getColumn("reason").setEditor(
				kdtEntries_reason_CellEditor);

		KDTDefaultCellEditor kdtEntries_adminOrg_CellEditor = (KDTDefaultCellEditor) this.kdtEntries
				.getColumn("adminOrg").getEditor();
		KDBizPromptBox kdtEntries_adminOrg_PromptBox = (KDBizPromptBox) kdtEntries_adminOrg_CellEditor
				.getComponent();
		kdtEntries_adminOrg_PromptBox.setEditFormat("$name$");
		kdtEntries_adminOrg_PromptBox.setCommitFormat("$name$");
		kdtEntries_adminOrg_PromptBox.setDisplayFormat("$name$");

		KDTextField kdtEntries_job_TextField = new KDTextField();
		kdtEntries_job_TextField.setName("kdtEntries_job_TextField");
		kdtEntries_job_TextField.setMaxLength(80);
		KDTDefaultCellEditor kdtEntries_job_CellEditor = new KDTDefaultCellEditor(
				kdtEntries_job_TextField);
		this.kdtEntries.getColumn("job").setEditor(kdtEntries_job_CellEditor);
	}

	public void loadFields() {
		super.loadFields();
//		cloneData = (RequireApplyBillInfo)this.editData.clone();
		this.pInfo = this.editData.getProposer();
		this.kdtEntries.getColumn("adminOrg").setRenderer(
				new OrgShortNameRender());
		this.kdtEntries.repaint();
	}

	@Override
	public void storeFields() {
		super.storeFields();
		IObjectCollection c = (IObjectCollection) this.editData.get("entries");
		RequireApplyBillEntryInfo info = null;

		for (int i = 0; i < this.kdtEntries.getRowCount(); i++) {
			info = (RequireApplyBillEntryInfo) c.getObject(i);
			if (this.kdtEntries.getRow(i).getCell("job").getValue() != null) {
				info.setJob(this.kdtEntries.getRow(i).getCell("job").getValue()
						.toString());
			}
		}
	}

	@Override
	public void onLoad() throws Exception {
		super.onLoad();
		
		this.comboSubmitType.setSelectedItem(ApproveStateEnum.WORKFLOW);
		this.comboSubmitType.setEditable(false);
		
		KDTPropertyChangeListener[] ls = this.kdtEntries
				.getListeners(KDTPropertyChangeListener.class);
		for (int i = 0; i < ls.length; i++) {
			this.kdtEntries.removeKDTPropertyChangeListener(ls[i]);
		}
		this.kdtEntries
				.addKDTPropertyChangeListener(new PositionPropertyChange(this));
	}

	@Override
	protected void kdtEntries_editStopped(KDTEditEvent e) throws Exception {
		super.kdtEntries_editStopped(e);
		int rowIndex = e.getRowIndex();
		int colIndex = e.getColIndex();
		if ("position"
				.equalsIgnoreCase(kdtEntries.getColumn(colIndex).getKey())) {
			kdtEntries
					.getCell(rowIndex, "job")
					.setValue(
							com.kingdee.bos.ui.face.UIRuleUtil
									.getString(com.kingdee.bos.ui.face.UIRuleUtil
											.getProperty(
													(com.kingdee.bos.dao.IObjectValue) kdtEntries
															.getCell(rowIndex,
																	"position")
															.getValue(),
													"job.name")));
		}
	}

	@Override
	public void actionRowAddNew_actionPerformed(ActionEvent e) throws Exception {
		super.actionRowAddNew_actionPerformed(e);
		for (int i = 0; i < this.kdtEntries.getRowCount(); ++i) {
			if ((this.kdtEntries.getCell(i, "adminOrg") != null)
					&& (this.kdtEntries.getCell(i, "adminOrg").getValue() == null)) {
				this.kdtEntries.getCell(i, "adminOrg").setValue(
						getAdminForHRandUserOrgRange());
			}
			this.kdtEntries.getCell(i, "adminOrg").setRenderer(
					new OrgShortNameRender());
			this.kdtEntries.repaint();
		}
	}

	private class PositionPropertyChange implements KDTPropertyChangeListener {
		private final RequireApplyBillEditUI ui;

		private PositionPropertyChange(RequireApplyBillEditUI ui) {
			this.ui = ui;
		}

		public void propertyChange(KDTPropertyChangeEvent evt) {
			int cPositionIndex = this.ui.kdtEntries.getColumnIndex("position");
			int cOutPositionIndex = this.ui.kdtEntries
					.getColumnIndex("outPosition");
			int cOutPosiTyptIndex = this.ui.kdtEntries
					.getColumnIndex("outPosiType");
			int cDeptIndex = this.ui.kdtEntries.getColumnIndex("adminOrg");
			if ((evt.getType() == 1) && (evt.getColIndex() == cPositionIndex)) {
				IRow row = this.ui.kdtEntries.getRow(evt.getRowIndex());
				PositionInfo pInfo = (PositionInfo) row.getCell(cPositionIndex)
						.getValue();
				if (pInfo != null) {
					DataBaseInfo adminOrgUnitInfo = pInfo.getAdminOrgUnit();
					String id = pInfo.getAdminOrgUnit().getId().toString();
					try {
						adminOrgUnitInfo = RecUtils.getDataBaseInfo(
								AdminOrgUnitFactory.getRemoteInstance(), id);
					} catch (Exception e) {
						ExceptionHandler.handle(e);
					}

					row.getCell(cDeptIndex).setValue(adminOrgUnitInfo);
					row.getCell(cDeptIndex).setRenderer(
							new OrgShortNameRender());
					this.ui.kdtEntries.repaint();
					row.getCell(cOutPositionIndex).setValue(null);
					row.getCell(cOutPosiTyptIndex).setValue(null);
				}
			}
			if ((evt.getType() == 1)
					&& (evt.getColIndex() == cOutPositionIndex)) {
				IRow row = this.ui.kdtEntries.getRow(evt.getRowIndex());
				OutPositionInfo opInfo = (OutPositionInfo) row.getCell(
						cOutPositionIndex).getValue();
				if (opInfo != null)
					row.getCell(cPositionIndex).setValue(null);
			}
		}
	}

	public void actionReferApplyBill_actionPerformed(ActionEvent e)
			throws Exception {
		KDCommonPromptDialog applyBillF7 = new KDCommonPromptDialog();
		applyBillF7.setEnabledMultiSelection(true);

		MetaDataPK queryPK = new MetaDataPK(this.queryFullName);
		QueryAgent queryAgent = new QueryAgent();
		queryAgent.setQueryInfoPK(queryPK);
		applyBillF7.setQueryInfo(queryAgent.getQueryInfo());

		EntityViewInfo viewInfo = new EntityViewInfo();
		FilterInfo filterInfo = null;

		if (!(RecUtils.getParamtForHRO())) {
			filterInfo = RecUtils.getHRAndAdminRangeFilterInfo("hrOrgUnit.id",
					"adminOrg.id");
		} else {
			filterInfo = new FilterInfo();
			FilterItemInfo item = RecUtils.getAdminRangeFilterItemInfo(null,
					"adminOrg.id");
			filterInfo.getFilterItems().add(item);
		}

		viewInfo.setFilter(filterInfo);
		applyBillF7.setEntityViewInfo(viewInfo);
		applyBillF7.show();

		if (applyBillF7.isCanceled())
			return;

		Object obj = applyBillF7.getData();
		if (!(RecUtils.objectIsNull(obj))) {
			HashSet requireSet = RecUtils.getObjectArrayIds(obj);

			setRowValueByEntryInfo(requireSet);
		}
	}

	private void setRowValueByEntryInfo(HashSet requireSet) throws Exception {
		RequireApplyBillEntryCollection entries = getEntryByRequireSet(requireSet);

		KDTable kdtEntries = getDetailTable();
		IRow row = null;

		int i = 0;
		for (int n = entries.size(); i < n; ++i) {
			RequireApplyBillEntryInfo entry = entries.get(i);
			row = kdtEntries.addRow();

			if (entry.getPosition() != null)
				row.getCell("position").setValue(entry.getPosition());
			if (entry.getOutPosition() != null)
				row.getCell("outPosition").setValue(entry.getOutPosition());
			if (entry.getOutPosiType() != null)
				row.getCell("outPosiType").setValue(entry.getOutPosiType());
			if (entry.getAdminOrg() != null)
				row.getCell("adminOrg").setValue(entry.getAdminOrg());
			if (entry.getGender() != null)
				row.getCell("gender").setValue(entry.getGender());
			if (entry.getAge() != null)
				row.getCell("age").setValue(entry.getAge());
			if (entry.getXueli() != null)
				row.getCell("xueli").setValue(entry.getXueli());
			if (entry.getSpecialty() != null)
				row.getCell("specialty").setValue(entry.getSpecialty());
			if (entry.getExp() != null)
				row.getCell("exp").setValue(entry.getExp());
			if (entry.getDuty() != null)
				row.getCell("duty").setValue(entry.getDuty());
			if (entry.getSalaryRange() != null)
				row.getCell("salaryRange").setValue(entry.getSalaryRange());
			if (entry.getAddress() != null)
				row.getCell("address").setValue(entry.getAddress());
			if (entry.getComeDate() != null)
				row.getCell("comeDate").setValue(entry.getComeDate());
			if (entry.getReason() != null)
				row.getCell("reason").setValue(entry.getReason());
			if (entry.getRemark() != null)
				row.getCell("remark").setValue(entry.getRemark());
			if (entry.getSpecial() != null)
				row.getCell("specialtyId").setValue(entry.getSpecial());
			if (entry.getSourceBill() != null) {
				row.getCell("sourceBill").setValue(entry.getSourceBill());
			}
			if (entry.getJob() != null) {
				row.getCell("job").setValue(entry.getJob());
			}
			String adjustNumber = entry.getAdjustNumber();
			if (!(StringUtils.isEmpty(adjustNumber)))
				row.getCell("needNumber").setValue(adjustNumber);
			else {
				row.getCell("needNumber").setValue(
						new Integer(entry.getNeedNumber()));
			}

			row.getCell("planNumber").setValue(
					new Integer(entry.getPlanNumber()));
			row.getCell("factNumber").setValue(
					new Integer(entry.getFactNumber()));
		}
	}

	private RequireApplyBillEntryCollection getEntryByRequireSet(HashSet idSet)
			throws Exception {
		EntityViewInfo viewInfo = new EntityViewInfo();

		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("id", idSet, CompareType.INCLUDE));

		viewInfo.setSelector(getSelectors());
		viewInfo.setFilter(filter);

		RequireApplyBillCollection requireColls = RequireApplyBillFactory
				.getRemoteInstance().getRequireApplyBillCollection(viewInfo);

		RequireApplyBillEntryCollection entries = new RequireApplyBillEntryCollection();
		int i = 0;
		for (int n = requireColls.size(); i < n; ++i) {
			RequireApplyBillInfo requireInfo = requireColls.get(i);
			RequireApplyBillEntryCollection entry = requireInfo.getEntries();
			int j = 0;
			for (int k = entry.size(); j < k; ++j) {
				RequireApplyBillEntryInfo entryInfo = entry.get(j);
				entryInfo.setSourceBill(requireInfo.getNumber());
				entries.add(entryInfo);
			}
		}
		return entries;
	}

	@Override
	public SelectorItemCollection getSelectors() {
		SelectorItemCollection selectors = super.getSelectors();
		selectors.add("entries.job");
		return selectors;
	}

	@Override
	public void actionSave_actionPerformed(ActionEvent e) throws Exception {
		super.actionSave_actionPerformed(e);
	}

	@Override
	public void actionSubmit_actionPerformed(ActionEvent e) throws Exception {
		super.actionSubmit_actionPerformed(e);
	}
}
